#include "TAccordion.h"
#include "TUI.h"
#include "../utils/Utils.h"

TAccordion* TAccordion::create(xml_node<> * pItem){
    
    TAccordion*  ret=TAccordion::create();
    ret->initWidthConf(pItem);
    return ret;
}

TAccordion::~TAccordion(){
    for(uint32 i=0;i<gPanels.size();i++){
        gPanels[i]->release();
    }
    
}


TAccordion::TAccordion(){
    m_pListener=NULL;
    m_pfnSelector=NULL;
    
    m_pCurPanel=NULL;
    m_pCurBtn=NULL;
}

void TAccordion::initWidthConf(xml_node<> * pItem){
    
    if(!pItem) return;
    CCSize size=readContainSize(pItem,getParent());
    if(size.width ==-1) size.width=getContentSize().width;
    if(size.height==-1) size.height=getContentSize().height;
    setContentSize(size);
    
    parseConf(pItem->first_node());
    
    
    onCreateComplete();
}


void TAccordion::onCreateComplete(){
    
    CCArray * children = getChildren();
    TPanelItem * node = 0;
    int count = children->count();
    CCPoint pos;
    for (int i = 0; i < count; ++i)
    {
        node = (TPanelItem*)children->objectAtIndex(i);
        if(node->tType==T_BUTTON_NAME){
            gBtns.push_back((TButton*)node);
            ((TButton*)node)->setTarget(this, menu_selector(TAccordion::onBtnClick));
        }else{
            node->retain();
            gPanels.push_back((TPanel*)node);
        }
        
    }
    
    
    if(gBtns.size()!=0){
        selectBtn(gBtns[0]);
    }
}

TPanel* TAccordion::getCurPanel(){
    
    return  m_pCurPanel;
    
}
TButton* TAccordion::getCurTButton(){
   
    return  m_pCurBtn;
}

void TAccordion::onBtnClick(CCObject* obj){
    selectBtn((TButton*)obj);
}
void TAccordion::clear(){
    for(uint32 i=0;i<gPanels.size();i++){
        if(gPanels[i]->getParent()){
            gPanels[i]->getParent()->removeChild(gPanels[i], true);
        }
    }
    
    for(uint32 i=0;i<gBtns.size();i++){
        gBtns[i]->setEnable(true);
    }
}


TButton* TAccordion::getBtnByTag(int tag){
    for(uint32 i=0;i<gBtns.size();i++){
         
        TPanelItem* item=getUI(gBtns[i]->linkPanel);
        if(item && tag==item->getTag()){
            return (gBtns[i]);
                
        }
             
        
    }
    return NULL;
}
void TAccordion::changeTab(int tag){
    
    TButton* btn=getBtnByTag(tag);
    
    if(!btn){
        return;
    }
    selectBtn(btn);
}
void TAccordion::changeIdx(int idx){
    TButton* btn=gBtns[idx];
    
    if(!btn){
        return;
    }
    selectBtn(btn);
}

void TAccordion::selectBtn(TButton* btn){
    if(btn->linkPanel.size()==0){
        return;
    }
    clear();
    btn->setEnable(false);
    
    m_pCurBtn=btn;
    
   
    TPanelItem* item=getUI( btn->linkPanel);
    if(item){
        m_pCurPanel=(TPanel*)item;
    }
   if(item && item->getParent()==NULL){
        addChild(item,-1);
    } 
    layout();
    
    if(m_pListener&& m_pfnSelector)
        (m_pListener->*m_pfnSelector)(btn);
}

void TAccordion::layout(){
    int curHeight=0;
    
     
    for(uint32 i=0;i<gBtns.size();i++){
        if(gBtns[i]){ 
            gBtns[i]->stopAllActions(); 
            CCPoint curPos=gBtns[i]->getPosition();
            curPos.y=-curHeight; 
            gBtns[i]->runAction(  CCEaseExponentialOut::create(CCMoveTo::create(0.2f, curPos))); 
            curHeight+=gBtns[i]->getContentSize().height;
                                
            if(getCurTButton()==gBtns[i]){ 
                TPanelItem* panel=getUI( gBtns[i]->linkPanel);
                if(panel){
                    panel->setPositionY(-curHeight);
                    curHeight+=panel->getContentSize().height;

                }
            }
            
        }
        
    }
  /*  for each(var btn:TButton in btns){
        TweenLite.to(btn,0.2,{y:curHeight});
        curHeight+=btn.height;
        if(curBtn==btn){
            var disp:DisplayObject= getPanelById(btn.linkPanel);
            disp.alpha=0;
            TweenLite.to(disp,0.2,{alpha:1});
            if(disp){
                disp.y=curHeight+btn.height;
                curHeight+=disp.height;
            }
            
        }
    }*/
    
}

void TAccordion::seTAccordionChangeTarget(CCObject*  listener,SEL_MenuHandler    selector){
    
    m_pListener=listener;
    m_pfnSelector=selector;
}

